<?php
namespace Home\Model;
use Think\Model;
//goods表操作类
class TypeModel extends Model{
    //首页商品分类查询方法
    public function GetLayer ($cate, $pid = 0, $name = 'lower',$html="",$level=0){
            $arr = array();
            foreach ($cate as $v) {
                if ($v['pid'] == $pid) {
                    $v['deep'] = $level;
                    $v['html'] = str_repeat($html, $level);
                    $v[$name] = $this->GetLayer($cate, $v['id'], $name, $html, $level+1);
                    $arr[] = $v;
                }
            }
            return $arr;
    }
    //--查询分类
    public function type_sec(){ 
        $data = $this->select();
        $res = $this->GetLayer($data);
        return $res;
        
    }
    //设置导航栏
    public function bar_sec(){
        $map['pid'] = ['eq',0];
        $list = $this->where($map)->limit(5)->select();
        return $list;
    }
    //列表页查询
    public function list_type(){
         $id = I('get.id');
            //查询商品类别表
            $tcom = $this->select();
            //查找路径(面包屑);
            function breadcrumbs(&$arr, $id)
            {
                foreach ($arr as $row)
                {
                    if ($row['id'] == $id)
                    {
                        $tmp[] = $row['name'];
                        if (!empty($row['pid']))
                        {
                            $tmp = array_merge(breadcrumbs($arr, $row['pid']), $tmp);
                        }
                    }
                }
                if (is_array($tmp) && !empty($tmp))
                {
                    return $tmp;
                }
                else
                {
                    return array();
                }
            }
             
            $arr = breadcrumbs($tcom, $id);
            $str = implode(' > ', $arr);
            //实例化goods表
            $goods = M('goods');
            //查询类别名
            $map['path'] = ['like',"%,$id,%"];
            $list = $this->where($map)->field('name')->select();
            $map = [];
            $map['id'] = ['eq',$id];
            $fname = $this->where($map)->field('name')->find();
            if($list){
                //查询其和其子类所有商品
                
                //获取其子类数量？
                $num = count($list);
                for ($i=0; $i < $num; $i++) { 
                    $m[] = $list[$i]['name'];
                }
            $m[] = $fname['name'];
            $map = [];
            $map['typeid'] = ['in',$m];
             //查询总数量
            $rows = $goods->where($map)->count();
            $page = new \Think\Page($rows , 12);
            $flist = $goods->where($map)->limit( $page->firstRow . ',' . $page->listRows )->select();
            //     for ($i=0; $i < $num; $i++) { 
            //         //设置查询条件
            //         $map = [];
            //         $map['typeid'] = ['eq',$list[$i]['name']];
            //         $rows = $goods->where($map)->count();
            //         $page = new \Think\Page($rows , 8);
            //         $glist[] = $goods->where($map)->limit( $page->firstRow . ',' . $page->listRows )->select();
            //     }
            //         $map = [];
            //         $map['id'] = ['eq',$id];
            //         $tlist = $this->where($map)->find();
            //         $map = [];
            //         $map['typeid'] = ['eq',$tlist['name']];
            //         //查询总数量
            //         $rows = $goods->where($map)->count();
            //         $page = new \Think\Page($rows , 10);
            //         $flist = $goods->where($map)->limit( $page->firstRow . ',' . $page->listRows )->select();
            //         foreach ($glist as $key => $value) {
            //         foreach ($value as $k => $val) {
            //             $flist[] = $val;
            //             // $flist['num'] = $list;
            //         }
            //     }
                
                        
                
            }else{
                $map = [];
                $map['id'] = ['eq',$id];
                $tlist = $this->where($map)->find();
                $map = [];
                $map['typeid'] = ['eq',$tlist['name']];
                //查询总数量
                $rows = $goods->where($map)->count();
                $page = new \Think\Page($rows ,12);
                $flist = $goods->where($map)->limit( $page->firstRow . ',' . $page->listRows )->select();
            }
            // //获取总行数
            // $rows = count($flist);
            //  // 实例化分页类
            // $page = new \Think\Page($rows , 5);
            // 执行分页查询
            // $flist = $this->limit( $page->firstRow . ',' . $page->listRows )->select();
            return [
                'str'=>$str,
                'list'=>$m,
                'glist'=>$flist,
                'show'=>$page->show(),
            ];
    }
    //查询子类方法
    public function type_son(){
        $id = I('get.id');
        // 设置查询条件
        $map['path'] = ['like',"%,$id,%"];
        $list = $this->where($map)->select();
        // $list = $this->GetLayer($list);
        return $list;
    }
}